<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="商户ID" prop="memberId">
        <el-input
          v-model="queryParams.memberId"
          placeholder="请输入商户ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="币种标识" prop="symbol">
        <MainCoin @change="onChainChange" :coin.sync="queryParams.symbol" :init="false" size="small"/>
      </el-form-item>
      <el-form-item label="主币种标识" prop="mainCoinSymbol">
        <MainCoin @change="onChainChange" :coin.sync="queryParams.mainCoinSymbol" type="0" :init="false" size="small"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
         <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['aipay:memberCoin:add']">新增</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5"></el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList">
      <el-table-column label="商户ID" align="center" prop="memberId" />
      <el-table-column label="币种ID" align="center" prop="coinId" />
      <el-table-column label="币种简称" align="center" prop="name" width="140"/>
      <el-table-column label="币种全称" align="center" prop="fullName" width="140"/>
      <el-table-column label="币种标识" align="center" prop="symbol" />
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          {{ typeMap[scope.row.type] }}
        </template>
      </el-table-column>
      <el-table-column label="币种精度" align="center" prop="decimals" />
      <el-table-column label="币种图片" align="center" prop="logo">
        <template slot-scope="scope">
          <el-image style="width: 40px; height: 40px" :src="scope.row.logo" ></el-image>
        </template>
      </el-table-column>
      <el-table-column label="主币种标识" align="center" prop="mainSymbol" />
      <el-table-column label="合约地址" align="center" prop="contractAddr">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleViewAddress(scope.row.contractAddr, '合约地址')">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="当前地址数量" align="center" prop="addressCount" />
      <el-table-column label="热钱包" align="center" prop="hotAddr">
        <template slot-scope="scope">
          <div v-if="scope.row.hotAddr">
            <div>
              <el-button size="mini" type="text" @click="handleViewAddress(scope.row.hotAddr, '热钱包地址')">查看地址</el-button>
            </div>
            <div>
              <el-button size="mini" type="text" @click="handleViewPrivate(scope.row)" v-hasPermi="['aipay:memberCoin:private']">查看私钥</el-button>
            </div>
          </div>
          <span v-else>无</span>
        </template>
      </el-table-column>
      <el-table-column label="GasLimit" align="center" prop="gasLimit" />
      <el-table-column label="推送阈值" align="center" prop="pushThreshold" />
      <el-table-column label="创建时间" align="center" prop="ctime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ctime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <div>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['aipay:memberCoin:edit']"
            >修改</el-button>
          </div>
          <div>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleWithdrawHot(scope.row)"
              v-hasPermi="['aipay:memberCoin:withdraw']"
            >热钱包提取</el-button>
          </div>
          <div>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdateAddress(scope.row)"
              v-hasPermi="['aipay:memberCoin:addressUpdate']"
            >更新热钱包</el-button>
          </div>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['aipay:memberCoin:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.limit"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" :close-on-click-modal="false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="商户ID" prop="memberId">
          <el-input v-model="form.memberId" placeholder="请输入商户ID" :disabled="form.id"/>
        </el-form-item>
        <el-form-item label="币种ID" prop="coinId" v-show="form.id">
          <el-input v-model="form.coinId" placeholder="请输入币种简称" disabled/>
        </el-form-item>
        <el-form-item label="币种简称" prop="name" v-show="form.id">
          <el-input v-model="form.name" placeholder="请输入币种简称" disabled/>
        </el-form-item>
        <el-form-item label="币种全称" prop="fullName" v-show="form.id">
          <el-input v-model="form.fullName" placeholder="请输入币种全称" disabled/>
        </el-form-item>
        <el-form-item label="币种标识" prop="symbol">
          <el-input v-model="form.symbol" placeholder="请输入币种标识" disabled v-if="form.id"/>
          <MainCoin @change="onChainChange" :coin.sync="form.symbol" :init="false" v-else/>
        </el-form-item>
        <el-form-item label="币种类型" prop="type" v-show="form.id">
          <el-input v-model="form.type" placeholder="请输入币种类型" disabled/>
        </el-form-item>
        <el-form-item label="币种图片" prop="logo" v-show="form.id">
          <el-input v-model="form.logo" placeholder="请输入币种图片" disabled/>
        </el-form-item>
        <el-form-item label="主币种ID" prop="mainCoinId" v-show="form.id">
          <el-input v-model="form.mainCoinId" placeholder="请输入主币种ID" disabled/>
        </el-form-item>
        <el-form-item label="主币种标识" prop="mainSymbol" v-show="form.id">
          <el-input v-model="form.mainSymbol" placeholder="请输入主币种标识" disabled/>
        </el-form-item>
        <el-form-item label="币种精度" prop="decimals" v-show="form.id">
          <el-input v-model="form.decimals" placeholder="请输入币种精度" disabled/>
        </el-form-item>
        <el-form-item label="合约地址" prop="contractAddr" v-show="form.id">
          <el-input v-model="form.contractAddr" placeholder="请输入合约地址" disabled/>
        </el-form-item>
        <el-form-item label="GasLimit" prop="gasLimit">
          <el-input v-model="form.gasLimit" placeholder="请输入GasLimit" />
        </el-form-item>
        <el-form-item label="推送阈值" prop="pushThreshold">
          <el-input v-model="form.pushThreshold" placeholder="请输入推送阈值" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="提取热钱包余额" :visible.sync="openHot" width="800px" :close-on-click-modal="false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="商户ID" prop="memberId">
          {{ form.memberId }}
        </el-form-item>
        <el-form-item label="币种ID" prop="coinId">
          {{ form.coinId }}
        </el-form-item>
        <el-form-item label="币种简称" prop="name">
          {{ form.name }}
        </el-form-item>
        <el-form-item label="币种全称" prop="fullName">
          {{ form.fullName }}
        </el-form-item>
        <el-form-item label="币种标识" prop="symbol">
          {{ form.symbol }}
        </el-form-item>
        <el-form-item label="币种余额" prop="symbol">
          {{ form.balance }}
          <div style="color: red;">此余额为链上时实余额, 提取未打币之前不会减少</div>
        </el-form-item>
        <el-form-item label="提取数量" prop="amount">
          <el-input v-model="withdrawForm.amount" placeholder="请输入提取数量" />
          <div style="color: red;">主币提取时, 实际收到的数量可能小于提取数量, 要减去Gas费</div>
        </el-form-item>
        <el-form-item label="收款地址" prop="toAddress">
          <el-input v-model="withdrawForm.toAddress" placeholder="请输入收款地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitWithdrawForm">确 定</el-button>
        <el-button @click="openHot = false">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="30%">
      <span>{{ dialogValue }}</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { listMemberCoin, getMemberCoin, withdrawMemberCoin, addMemberCoin, updateMemberCoin, delMemberCoin, updateAddress, getPrivateKey  } from "@/api/aipay/memberCoin";

export default {
  name: "Resource",
  dicts: ['sys_bool_status','sys_open_close'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //表格数据
      postList: [],
      roleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dialogVisible: false,
      dialogTitle: '',
      dialogValue: '',
      activeName: "public",
      openHot: false,
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        status: undefined,
        memberId: undefined,
        symbol: undefined,
        mainCoinSymbol: undefined
      },
      typeMap: {
        0: '主币',
        1: '代币'
      },
      statusMap: {
        0: '关闭',
        1: '开启'
      },
      // 表单参数
      form: {},
      withdrawForm: {},
      // 表单校验
      rules: {
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listMemberCoin(this.queryParams).then(response => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    onChainChange(value){
      console.log(value)
    },
    handleViewAddress(row, title){
      this.dialogTitle = title
      this.dialogValue = row
      this.dialogVisible = true
    },
    handleViewPrivate(row){
      getPrivateKey(row.id).then(res => {
        this.dialogTitle = "私钥"
        this.dialogValue = res.msg
        this.dialogVisible = true
      })
    },
    handleWithdrawHot(row){
      this.withdrawForm = {}
      getMemberCoin(row.id).then(response => {
        this.form = response.data;
        this.openHot = true
      });
    },
    submitWithdrawForm(){
      let that = this
      this.withdrawForm.id = this.form.id
      this.$confirm('是否确定提取热钱包余额?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return withdrawMemberCoin(that.withdrawForm);
        }).then(() => {
          that.openHot = false
          that.msgSuccess("提取成功,请等待系统打币");
        }).catch(() => {});
    },
    handleUpdateAddress(row){
      if(row.type == 1) {
        let form = {
          id: row.id,
          isFollowMain: false
        }
        let that = this
        this.$confirm('是否跟随主币热钱包地址?', "警告", {
          distinguishCancelAndClose: true,
          confirmButtonText: "是",
          cancelButtonText: "否",
          type: "warning"
        }).then(function() {
          form.isFollowMain = true
          updateAddress(form).then(res => {
            that.msgSuccess("更新成功");
            that.getList();
          })
        }).catch((action) => {
          form.isFollowMain = false
          if (action === 'cancel') {
            updateAddress(form).then(res => {
              that.msgSuccess("更新成功");
              that.getList();
            })
          }
        });
      } else {
        let form = {
          id: row.id,
          isFollowMain: false
        }
        this.$confirm('是否确定更新热钱包地址?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return updateAddress(form);
        }).then(() => {
          this.getList();
          this.msgSuccess("更新成功");
        }).catch(() => {});
      }
      
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加币种";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getMemberCoin(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改币种";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateMemberCoin(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMemberCoin(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMemberCoin(row.id);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    }
    
  }
};
</script>
